package com.jnu.hospitalexamination.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jnu.hospitalexamination.common.Result;

import com.jnu.hospitalexamination.entity.Orders;
import com.jnu.hospitalexamination.entity.vo.UserOrderVO;
import com.jnu.hospitalexamination.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;


//@CrossOrigin(origins = "*",allowedHeaders = "*",exposedHeaders = "*")
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/hello")
    public Result hello() {
        return Result.success(200,"hello",null);
    }

    /**
     * 添加预约订单项目
     * @param userId 用户ID
     * @param hpId 预约医院ID
     * @param setmealId 套餐ID
     * @param orderDate 预约日期
     * @return Result 成功状态码200，失败状态码4001，数据，错误信息
     */
    @Transactional
    @RequestMapping("/addOrder")
    public Result addOrder(@RequestParam Integer userId,@RequestParam Integer hpId,@RequestParam Integer setmealId,@RequestParam("orderDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date orderDate) {
        Orders orders = new Orders();
        orders.setHpid(hpId);
        orders.setSmid(setmealId);
        orders.setUserid(userId);
        orders.setState(1);
        orders.setOrderdate(orderDate);
        try {
            Boolean save = ordersService.save(orders);
            return Result.success(200,"添加预约订单成功",save);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(4001,"添加预约订单失败"+e.getMessage(),null);
        }
    }

    /**
     * 取消预约,根据orderId删除订单
     * @param orderId 订单ID
     * @return Result 成功状态码200，失败状态码4002，数据，错误信息
     */
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(@RequestParam Integer orderId) {
        boolean b = ordersService.removeById(orderId);
        if(b) {
            return Result.success(200,"取消预约成功",b);
        }else {
            return Result.error(4002,"取消预约失败",b);
        }
    }

    @RequestMapping("/getUserOrderList")
    public Result getOrderList(){
        return Result.success(200,"用户预约列表",ordersService.getUserOrderList());
    }


    //选择预约日期

    /**
     * 根据医院ID和日期获取当天预约人数
     * @param hpId 医院ID
     * @param orderDate 预约日期
     * @return 当天预约人数
     */
    @RequestMapping("/selectDate")
    public Result selectDate(@RequestParam("hpId") Integer hpId, @RequestParam("orderDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate orderDate) {
        try {
            return Result.success(200,orderDate+"预约人数",ordersService.getOrderNum(hpId,orderDate));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(4003,"打开预约列表失败"+e.getMessage(),null);
        }
    }

    // 我的预约

    /**
     * 根据用户ID获取预约列表
     * @param userId
     * @return 预约列表
     */
    @RequestMapping("/myOrder")
    public Result myOrder(@RequestParam Integer userId) {
        try {
            return Result.success(200,"我的预约",ordersService.getOrderList(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(4004,"打开我的预约失败"+e.getMessage(),null);
        }
    }


    /**
     * 根据订单ID更新order状态
     * @param orderId
     * @param state
     * @return
     */
    @RequestMapping("/archiveOrder")
    @Transactional
    public Result archiveOrder(@RequestParam Integer orderId,Integer state) {
        try {
            LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Orders::getOrderid,orderId);
            wrapper.set(Orders::getState,state);
            boolean update = ordersService.update(wrapper);
            return Result.success(200,"更新订单成功",update);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(4005,"更新订单失败"+e.getMessage(),null);
        }
    }

}
